Ext.define('AM.view.ProfessionalView', {
		extend: 'Ext.grid.Panel',
		alias : 'widget.professionalView',
		store : 'ProfessionalStore',
		columnLines:true,
//		selModel : Ext.create('Ext.selection.CheckboxModel'), 
		selModel: {
	        injectCheckbox: 0,
	        mode: "SIMPLE"   //"SINGLE"/"SIMPLE"/"MULTI"
	       
   		 },
   		selType: "checkboxmodel",
    	dockedItems: [{
        xtype: 'pagingtoolbar',
        dock: 'bottom',
        store : 'ProfessionalStore',
        displayInfo: true
    }],
		columns: [	
			        {text: '序号',  dataIndex: 'id',width:50,align:'center'},
			        { text: '名称', dataIndex: 'name', width:150,align:'center'},
//			        { text: '科室', dataIndex: 'department.name', width:200,align:'center'},
			        { text: '等级', dataIndex: 'grade', width:80,align:'center'}
			     ],
			   tbar: [
					  { xtype: 'button', id:'tab-9_add',text: '增加',handler: addProfessional,icon: 'Ext/restful/images/add.png'
					  },
					  { xtype: 'button', id:'tab-9_edit', text: '修改',handler: editProfessional,icon: 'Ext/restful/images/cog_edit.png'
					  },
					  { xtype: 'button', id:'tab-9_delete',text: '删除',handler: deleteProfessional,icon: 'Ext/restful/images/delete.png'
					  }
//					   {
//					     xtype : "combobox",
//					     allowBlank:false,
//					     labelAlign :"right",
//						 fieldLabel: '选择科室',
//						 id:'search_profina',
//						 store :Ext.create('AM.store.TargetDepartStore'),
//						 displayField: 'name',
//						 valueField: 'id',
//					     width:300
//					  },
//					  { xtype: 'button', text: '查询',id:'tab-9_search',handler: searchProfessional,icon : 'Ext/restful/images/find.png'
//					  }
					]
		
})
function searchProfessional(){
			var grid=this.ownerCt.ownerCt;
			var depart=  Ext.getCmp('search_profina').getValue();
			  grid.getStore().on('beforeload', function (store, options) {
	          var new_params = {depart:depart}
	                Ext.apply(store.proxy.extraParams, new_params);
	       });//此处为加载条件
			  grid.getStore().reload();
					
}
   function addProfessional() {
		 var myform1= new Ext.form.Panel({
				    bodyPadding:20,
			        labelWidth : 65,
				    items: [{   
					   		      allowBlank:false,
					   			  xtype : "textfield",
					   		      fieldLabel: '职称名称',
					   			  labelAlign :"right",
							      name: 'name',
							      width:300
							},
//								{
//								 xtype : "combobox",
//								 allowBlank:false,
//							     emptyText:'请选择科室',
//								 selectOnFocus:true,
//								 width:300,
//								 forceSelection: true,
//								 queryMode: 'local',
//								 labelAlign :"right",
//								 fieldLabel: '科室',
//								 name: 'department.id',
//								 store :"DepartmentStore",
//								 displayField: 'name',
//								 valueField: 'id'
//							},
								{
								 xtype : "combobox",
								 allowBlank:false,
								 width:300,
							     emptyText:'请选择等级',
								 selectOnFocus:true,
								 forceSelection: true,
								 queryMode: 'local',
								 labelAlign :"right",
								 fieldLabel: '等级',
								 name: 'grade',
								 store :Ext.create('Ext.data.Store', {
										 fields: ['id', 'name'],
										 data : [
										 		{"id":"无职称", "name":"无职称"},
										        {"id":"初级", "name":"初级"},
										        {"id":"中级", "name":"中级"},
										        {"id":"副高级", "name":"副高级"},
										        {"id":"高级", "name":"高级"}
										    ]
										}),
								 displayField: 'name',
								 valueField: 'id'
								
						}]		        	
				        	       

		 })
		    var grid=this.ownerCt.ownerCt;
			var win = new Ext.Window({
				title : "添加职称",
				width : 500,
				height : 200,
				frame:true,
				layout:'fit',
				items : [ myform1
							],
				buttons : [{
							text : "确定",
							handler : function() {
								win.getComponent(0).getForm().submit({
											 url : 'professional_addProfessional',
											 success : function() {
												Ext.Msg.alert('提示', '添加成功');
												win.close();
												grid.getStore().reload();
											},
											failure : function() {
												Ext.Msg.alert('错误', '添加失败');
											},
											waitMsg : "请稍后，正在保存。。。。"
	
										});
							}
						}, {
							text : "重置",
							handler : function() {
								win.getComponent(0).getForm().reset();
							}
						}, {
							text : "取消",
							handler : function() {
												win.close();
											
							}
						}]
			});
		win.show();
	}



   function editProfessional() {
		 var myform1= new Ext.form.Panel({
			       
			         bodyPadding: 20,
			        labelWidth : 65,
			        items: [{   
					   		      allowBlank:false,
					   			  xtype : "textfield",
					   		      fieldLabel: '职称名称',
					   			  labelAlign :"right",
							      name: 'name',
							      width:300
							},
//								{
//								 xtype : "combobox",
//								 allowBlank:false,
//							     emptyText:'请选择科室',
//								 selectOnFocus:true,
//								 width:300,
//								 forceSelection: true,
//								 queryMode: 'local',
//								 labelAlign :"right",
//								 fieldLabel: '科室',
//								 name: 'department.id',
//								 store :"DepartmentStore",
//								 displayField: 'name',
//								 valueField: 'id'
//							},
								{
								 xtype : "combobox",
								 allowBlank:false,
								 width:300,
							     emptyText:'请选择等级',
								 selectOnFocus:true,
								 forceSelection: true,
								 queryMode: 'local',
								 labelAlign :"right",
								 fieldLabel: '等级',
								 name: 'grade',
								 store :Ext.create('Ext.data.Store', {
										 fields: ['id', 'name'],
										 data : [
										        {"id":"初级", "name":"初级"},
										        {"id":"中级", "name":"中级"},
										        {"id":"副高级", "name":"副高级"},
										        {"id":"高级", "name":"高级"}
										    ]
										}),
								 displayField: 'name',
								 valueField: 'id'
								
						},{
							xtype : "textfield",
					   	    fieldLabel: 'ID',
					   	    labelAlign :"right",
						    name: 'id',
						    hidden:true,
							width:300
						}]		        
			              	
		
		 })
		 	var grid=this.ownerCt.ownerCt;
			var win = new Ext.Window({
				title : "修改职称",
				width : 500,
				height : 200,
				frame:true,
				layout:'fit',
				items : [ myform1
							],
				buttons : [{
							text : "确定",
							handler : function() {
								win.getComponent(0).getForm().submit({
											 url : 'professional_editProfessional',
											 success : function() {
												Ext.Msg.alert('提示', '修改成功');
												win.close();
												grid.getStore().reload();
												grid.getSelectionModel().deselectAll();//去除选择
											},
											failure : function() {
												Ext.Msg.alert('错误', '修改失败');
											},
											waitMsg : "请稍后，正在保存。。。。"
	
										});
							}
						}, {
							text : "重置",
							handler : function() {
								win.getComponent(0).getForm().reset();
							}
						}, {
							text : "取消",
							handler : function() {
												win.close();
											
							}
						}]
			});
		var selections = grid.getSelectionModel().getSelection();
						if(selections.length==0){
							Ext.MessageBox.alert("提示","请选择要编辑的一行！");
						}else if (selections.length > 1) {
							Ext.MessageBox.alert("提示", "请检查您是否选中了多行！");
							return;
						} else{
							var record = selections[0];
						//这里是修改操作的实现部分
							win.getComponent(0).getForm().loadRecord(record);
							win.show();
						}
	}

	function deleteProfessional(){
				 var grid=this.ownerCt.ownerCt;
			     var selections = grid.getSelectionModel().getSelection();
					if(selections){
					if(selections.length==0){
							Ext.MessageBox.alert("提示","请检查是否选中某一行！");
						}else if (selections.length > 1) {
							Ext.MessageBox.alert("提示", "请检查您是否选中了多行！");
							return;
						} else{
							Ext.MessageBox.confirm("提示", "确认要删除 ？", function(btn) {
							if (btn == 'yes') {			
							var record = selections[0];
								Ext.Ajax.request({
										url : 'professional_deleteProfessional',
										success : function() {
											Ext.MessageBox.alert("提示",
													"删除" + record.get("name")+ "成功！");
												grid.getStore().remove(selections );
												grid.getStore().reload();
										},
										failure : function() {
											Ext.MessageBox.alert("提示", "职称被引用，不能删除！");
										},
										params : {
											id : record.get("id")
										}
									});			
								}
					  	})
                   	} 
 			   
		   }				
	}
	 	

	